package com.shuoyue.adven.main.appcontrol;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

public class DevicesTool {
    /**
     * 设置下次开关机
     *
     * @param context
     */
    public static void setPower(Context context) {
        String SET_POWER_ON_OFF = "android.intent.action.setpoweronoff";   //携带的数据格式为：
        int[] timeonArray = {2020, 12, 4, 11, 10};
        //下次开机具体日期时间，即在2017.7.13 8:30开机
        int[] timeoffArray = {2020, 12, 4, 11, 8};
        //下次关机的时间
        Intent intent = new Intent();
        intent.setAction(SET_POWER_ON_OFF);
        intent.putExtra("timeon", timeonArray);
        intent.putExtra("timeoff", timeoffArray);
        intent.putExtra("enable", true);
        // 使能开关机，true为打开，false为关闭 10.
        context.sendBroadcast(intent);
    }

    //pkg 为要守护的app包名，取消守护传参空字符 ""
    public static void setAppKeepLive(String pkg) {
        Process process = null;
        String cmd1 = "setprop persist.lgo.nooperateStartPkg xxx";
        String cmd2 = "setprop persist.lgo.keepNopAliveTimeSec xxx";
        try {
            if (TextUtils.isEmpty(pkg)) {
                cmd1 = cmd1.replace("xxx", "");

                cmd2 = cmd2.replace("xxx", "-1");
            } else {
                cmd1 = cmd1.replace("xxx", pkg);
                cmd2 = cmd2.replace("xxx", "15");
            }
            process = Runtime.getRuntime().exec(cmd1);
            process = Runtime.getRuntime().exec(cmd2);
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
    }

}
